<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>抽奖平台 Made By Muc</title>
    <link rel="icon" href="./img/title.bmp">
    </link>
    <link rel="stylesheet" type="text/css" href="./css/rollCall.css">
    <script type="text/javascript" src="./js/xlsx.core.min.js" async></script>
</head>

<body>
    <!-- <meter id="m1" value="0" min="0" max="80"></meter> -->
    <div class=frame>
        <div id="content-box"></div>
        <div id="group-hide-box"></div>
        <button id="rollBtn" onClick="stopFlash()">开 始</button>
    </div>
</body>

<script language="javascript" type="text/javascript">
var studentsDict = {},
    status = 1,
    getTimer; // 随机获取学生名循环

var g_aStudents = []; // 以二进制流方式读取得到整份excel表格对象

var dropPrompt = "请将Excel表格\n拖拽到此处",
    dragenterPrompt = "请释放\n手中的鼠标";


window.onload = function() {
    // 隐藏分组元素
    document.getElementById("group-hide-box").style.display="none";

    var oBox = document.getElementById("content-box"),
        oRollBtn = document.getElementById("rollBtn");

    document.onkeydown = function(event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        // 按下任意按钮，停止
        if (e.key === 's') {
            oRollBtn.click();
        }
    };

    oBox.innerHTML = dropPrompt;

    oBox.ondragenter = function() {
        oBox.innerHTML = dragenterPrompt;
    };
    oBox.ondragover = function() {
        return false; //进入子集的时候 会触发ondragover 频繁触发不给ondrop机会
    };
    // oBox.ondragleave = function() {
    //     oBox.innerHTML = dropPrompt;
    // };
    oBox.ondrop = function(ev) {
        var oFile = ev.dataTransfer.files[0],
            reader = new FileReader();

        // 读取成功后的处理
        reader.readAsBinaryString(oFile);
        reader.onload = function(ev) {
            // console.log("读取结果：", this.result); //当读取完成之后会回调这个函数，然后此时文件的内容存储到了result中。直接操作即可。
            var data = ev.target.result,
                workbook = XLSX.read(data, {
                    type: 'binary'
                });

            // 表格的表格范围，可用于判断表头是否数量是否正确
            var fromTo = '';

            // 遍历每张表读取
            for (var sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    fromTo = workbook.Sheets[sheet]['!ref'];
                    g_aStudents = g_aStudents.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                    break; // 只取第一张表
                }
            }

            goname();

        };
        return false;
    };
};

/**
 * @Describle [周期性抽取学生]
 * @Author    Muc
 * @DateTime  2019-03-05
 */
function goname() {
    getTimer = setInterval(startFlash, 80);
}

/**
 * @Describle [展示被抽到的学生]
 * @Author    Muc
 * @DateTime  2019-03-05
 */
function startFlash() {
    var aTmp = getStudent();
    document.getElementById("content-box").innerHTML = aTmp[0] + '\n'  +aTmp[1];
    document.getElementById("group-hide-box").innerHTML = aTmp[2];

    setTimeout("startFlash", 80);
}

/**
 * @Describle [暂定抽取]
 * @Author    Muc
 * @DateTime  2019-03-05
 */
function stopFlash() {
    // xls文件导入后才能使用“点名”功能
    if (g_aStudents.length) {
        if (status == 1) {
            clearInterval(getTimer);
            status = 0;
            // console.log("1")
            signForFilterClass();
        } else {
            goname();
            status = 1;
            // console.log("2")
        }

    }

}

/**
 * @Describle [标记已抽过的班级，将数据进行过滤]
 * @Author    Muc
 * @DateTime  2019-03-05
 * @return    {[序号:1, 内容:XXX, 备注:XXX], ...}
 */
function signForFilterClass() {
    // xls文件导入后才能使用“点名”功能
    var group_name = document.getElementById("group-hide-box").innerHTML;
    var aTmp = [], inxTmp = 0;

    // 遍历g_aStudents，过滤掉同分组的数据
    for (var inx=0, len=g_aStudents.length; inx<len; inx++) {
        if (g_aStudents[inx]['分组'] != group_name) {
            aTmp[inxTmp] = g_aStudents[inx];
            ++inxTmp;
        }
    }

    g_aStudents = aTmp;
}

/**
 * @Describle [从学生字典中，随机获取学生]
 * @Author    Muc
 * @DateTime  2019-03-05
 * @return    {[序号:1, 内容:XXX, 备注:XXX], ...}
 */
function getStudent() {
    var maxNum = g_aStudents.length - 1,
        minNum = 0,
        random_inx = Math.floor(Math.random() * (maxNum - minNum+1) + minNum);

    var content = g_aStudents[random_inx]["内容"],
        notes = g_aStudents[random_inx]["备注"]?g_aStudents[random_inx]["备注"]:"",
        group =  g_aStudents[random_inx]["分组"];

    return new Array(content, notes, group);
}

/**
 * @Describle [字典键数计算]
 * @Author    Muc
 * @DateTime  2019-03-05
 * @param     {[dict]}    dict   [学生字典]
 * @return    {[string]}         [eg. 1号 王爱国]
 */
function dictKeysCount(dict) {
    var dictLength = 0;
    for (var item in dict) {
        ++dictLength;
    }
    return dictLength;
}

</script>

</html>
